#pragma once
#include <EGL/egl.h>  // EGL支持
#include <GLES3/gl3.h>
#include <android/log.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>  // ANativeWindow_fromSurface
#include <fcntl.h>
#include <unistd.h>

#include <atomic>
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <thread>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <unistd.h>
}
#include <atomic>
#include <condition_variable>
#include <mutex>

#include "audio_codec.h"
#include "include/safe_queue.h"
#include "parse.h"
#include "third_party/threadpool/include/threadpool.h"
#include "video_codec.h"
#include "video_render.h"
namespace codec {
class PlayManager {
 private:
  std::unique_ptr<ThreadPool> pools = std::make_unique<ThreadPool>();
  std::unique_ptr<Parse> parse_ = std::make_unique<Parse>();
  std::unique_ptr<VideoCodec> videoCodec_ = std::make_unique<VideoCodec>();
  std::unique_ptr<AudioCodec> audioCodec_ = std::make_unique<AudioCodec>();
  std::unique_ptr<VideoRender> videoRender_ = std::make_unique<VideoRender>();
  using progressCallbackType = std::function<void(double, double)>;
  progressCallbackType progressCallback_;

  // 全局队列
  SafeQueue<AVPacket *> video_packet_queue;  // 视频包队列
  SafeQueue<AVPacket *> audio_packet_queue;  // 音频包队列
  SafeQueue<AVFrame *> video_frame_queue;    // 视频帧队列
  std::atomic<bool> abort_ = false;
  double lastPlaytime_ = 0.00;
  float lastSeekTime = 0.00;

  std::atomic<bool> paused_ = false;
  std::mutex paused_mutex_;
  std::condition_variable paused_cv_, audio_cv_;

  // 新增跳转控制变量
  std::atomic<bool> seeking_{false};
  std::atomic<int64_t> seek_target_us_{0};
  std::mutex seek_mutex_;
  std::condition_variable parse_cv, seek_cv_;

 public:
  PlayManager();
  std::map<std::string, std::string> prepare(ANativeWindow *window,
                                             const std::string &vertexCode,
                                             const std::string &fragmentCode);
  void start();
  bool clearOver() {
    return video_packet_queue.empty() && audio_packet_queue.empty() &&
           video_frame_queue.empty();
  };
  void setDataSource(int fd);
  void pause() {
    std::lock_guard<std::mutex> lock(paused_mutex_);
    paused_ = true;
    audioCodec_->pause();
  }

  void resume() {
    {
      std::lock_guard<std::mutex> lock(paused_mutex_);
      paused_ = false;
      audioCodec_->resume();
    }
    audio_cv_.notify_all();
  }
  void seekTo(float position);
  void clearQueues();
  void release();
  void changeSurface(int width, int height, bool isFullScreen);
  void registerProgressCallback(std::function<void(double, double)> cb) {
    progressCallback_ = cb;
  }
  void unregisterProgressCallback() { progressCallback_ = nullptr; }
  void onInternalProgressUpdated(double progress, double duration) {
    if (progressCallback_) {
      progressCallback_(progress, duration);
    }
  }
};
}  // namespace codec